/*
 * Created by JFormDesigner on Wed Jul 03 21:59:16 CST 2024
 */

package scct.zhang.ui;

import scct.zhang.dao.UserDao;
import scct.zhang.utils.Tools;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author 张
 */
public class FromSign extends JFrame {
    public FromSign() {
        initComponents();
    }

    private void btnSign(ActionEvent e) {
        // TODO add your code here
        String name = txtUser.getText();
        String pass = txtpass.getText();
        String createtime = Tools.getCreatetime();
        System.out.println(name + "," + pass);
//        name.matches("正则表达式")返回true成功，false失败
        if (name.isEmpty() || !name.matches("[a-zA-Z0-9_\\u4e00-\\u9fa5]{1,10}$")){
            JOptionPane.showMessageDialog(null,"你输入的用户名不合法，请重新检查后输入");
            return;
        }
        if (pass.isEmpty() || !pass.matches("[a-zA-Z0-9_\\u4e00-\\u9fa5]{1,10}$")){
            JOptionPane.showMessageDialog(null,"你输入的密码不合法，请重新检查后输入");
            return;
        }
        System.out.println(name + ","+pass);
        UserDao userDao = new UserDao();
        if (userDao.isDupname(name)){
            JOptionPane.showMessageDialog(this,"用户名重复，请重新添加");
            return;
        }
        if (userDao.addUser(name,pass,createtime)){
            JOptionPane.showMessageDialog(this,"注册成功");
        }
        this.dispose();

            FormMain formMain = new FormMain();
            formMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            formMain.setSize(1000,700);
//            居中
            formMain.setLocationRelativeTo(null);
//            formMain.set
            formMain.setVisible(true);
//            隐藏当前窗体
            this.dispose();
//            进入主窗体

    }

    private void btnCancel(ActionEvent e) {
        // TODO add your code here
        this.dispose();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhang
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        txtpass = new JPasswordField();
        txtUser = new JTextField();
        btnSign = new JButton();
        btnCancel = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("Sign Up");
        label1.setFont(label1.getFont().deriveFont(Font.BOLD|Font.ITALIC, label1.getFont().getSize() + 11f));
        contentPane.add(label1);
        label1.setBounds(150, 30, 105, 30);

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d\uff1a");
        contentPane.add(label2);
        label2.setBounds(90, 90, 51, 17);

        //---- label3 ----
        label3.setText("\u5bc6\u7801\uff1a");
        contentPane.add(label3);
        label3.setBounds(90, 150, 38, 17);
        contentPane.add(txtpass);
        txtpass.setBounds(175, 140, 145, 30);
        contentPane.add(txtUser);
        txtUser.setBounds(175, 85, 145, txtUser.getPreferredSize().height);

        //---- btnSign ----
        btnSign.setText("\u6ce8\u518c");
        btnSign.addActionListener(e -> btnSign(e));
        contentPane.add(btnSign);
        btnSign.setBounds(new Rectangle(new Point(115, 200), btnSign.getPreferredSize()));

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        btnCancel.addActionListener(e -> btnCancel(e));
        contentPane.add(btnCancel);
        btnCancel.setBounds(new Rectangle(new Point(240, 200), btnCancel.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(400, 300);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhang
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JPasswordField txtpass;
    private JTextField txtUser;
    private JButton btnSign;
    private JButton btnCancel;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
